package com.delaphone.flocash.web.service;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;

/**
 *
 * @author tamdx, dongtv
 */
public class MailServiceImp implements MailService {

    private String host;
    private String port;
    private String from;
    private String password;
    private boolean sslEnabled;
    private Logger logger = Logger.getLogger("MailService");

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public boolean isSslEnabled() {
        return sslEnabled;
    }

    public void setSslEnabled(boolean sslEnabled) {
        this.sslEnabled = sslEnabled;
    }

    public void sendMail(String to, String subject, String content) {
        try {
            Session session = getSession(from.trim(), password.trim());
            Message msg = new MimeMessage(session);            
            msg.setFrom(new InternetAddress(from));
            msg.setRecipients(RecipientType.TO, InternetAddress.parse(to, false));
            msg.setSubject(subject);
            msg.setContent(content, "text/plain");
            Transport.send(msg);
        } catch (Exception ex) {
            logger.error("Send mail error:", ex);
            logger.info("Mail Host:" + host + ",Port=" + port + ",User=" + from);
        }
    }

    private Session getSession(String username, String password) {
        Authenticator authenticator = new Authenticator(username, password);
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", "true");
        if (sslEnabled) {
            properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        }
        properties.setProperty("mail.smtp.host", getHost().trim());
        properties.setProperty("mail.smtp.port", getPort().trim());
        return Session.getInstance(properties, authenticator);
    }

    private class Authenticator extends javax.mail.Authenticator {

        private PasswordAuthentication authentication;

        public Authenticator(String username, String password) {
            authentication = new PasswordAuthentication(username, password);
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return authentication;
        }
    }

    public static void main(String args[]) {
        MailServiceImp t = new MailServiceImp();
        t.setHost("smtpout.secureserver.net");
        t.setFrom("flosupport@flocash.com");
        t.setPort("110");
        t.setSslEnabled(false);
        t.setPassword("ju89df");
        t.sendMail("aladanh66@gmail.com", "This is test title", "This is test body!");
    }
}
